package com.fun.feifun.modules.business.model;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fun.feifun.base.BaseEntity;
import com.fun.feifun.modules.business.constant.OrderPayStatusEnum;
import com.fun.feifun.modules.business.constant.OrderStatusEnum;
import com.fun.feifun.modules.business.model.param.BusinessPlaceOrderParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * <p>
 * 系统用户
 * </p>
 *
 * @author wkf
 * @since 2024-06-04
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@ApiModel(value="BusinessOrder对象", description="系统用户")
public class BusinessOrder extends BaseEntity {

    private static final long serialVersionUID=1L;

    @ApiModelProperty(value = "订单编号")
    private String code;

    @ApiModelProperty(value = "商品id ")
    private Long goodsId;

    @ApiModelProperty(value = "商品运营商")
    private Integer goodsOperators;

    @ApiModelProperty(value = "商品编码")
    private String goodsCode;

    @ApiModelProperty(value = "商品名称")
    private String goodsName;

    @ApiModelProperty(value = "用户id")
    private Long businessUserId;

    @ApiModelProperty(value = "用户编码")
    private String businessUserCode;

    @ApiModelProperty(value = "用户名")
    private String businessUserName;

    @ApiModelProperty(value = "用户名称")
    private String businessUserNike;

    @ApiModelProperty(value = "充值目标账号")
    private String rechargeAccount;

    @ApiModelProperty(value = "商品金额")
    private BigDecimal goodsAmount;

    @ApiModelProperty(value = "平台手续金额")
    private BigDecimal feesAmount;

    @ApiModelProperty(value = "供货商id")
    private Long supplierId;

    @ApiModelProperty(value = "供货商编码")
    private String supplierCode;

    @ApiModelProperty(value = "供货商名称")
    private String supplierName;

    @ApiModelProperty(value = "供货商手续费")
    private BigDecimal supplierFees;

    @ApiModelProperty(value = "平台商品编号")
    private String productCode;

    @ApiModelProperty(value = "订单状态 1初始化 2匹配成功 3匹配失败 4支付中 5支付成功 6支付失败")
    private Integer state;

    @ApiModelProperty(value = "支付状态 0支付中 1支付成功 -1支付失败")
    private Integer payState;

    @ApiModelProperty(value = "备注")
    private String remake;

    @ApiModelProperty(value = "支付时间")
    private LocalDateTime payTime;

    @ApiModelProperty(value = "回单时间")
    private LocalDateTime returnTime;

    @ApiModelProperty(value = "订单结束时间")
    private LocalDateTime endTime;

    @ApiModelProperty(value = "花费时间")
    @TableField(exist = false)
    private String costTime;

    @ApiModelProperty(value = "外部订单编号")
    private String outsideOrderCode;

    @ApiModelProperty(value = "代理用户订单编码")
    private String userOrderCode;

    @ApiModelProperty(value = "回调接口地址")
    private String backUrl;

    @ApiModelProperty(value = "是否返回给用户 0未返回 1返回成功 -1返回失败")
    private Integer isBack;

    @ApiModelProperty(value = "返回信息")
    private String backMsg;


    public String getCostTime(){
        LocalDateTime start=this.createTime;
        LocalDateTime end=returnTime!=null?returnTime:LocalDateTime.now();
        long totalSeconds = ChronoUnit.SECONDS.between(start, end);
        long days = totalSeconds / (24 * 60 * 60);
        long hours = (totalSeconds % (24 * 60 * 60)) / (60 * 60);
        long minutes = (totalSeconds % (60 * 60)) / 60;
        long seconds = totalSeconds % 60;
        String timeDifference ="";
        if(days==0l){
            timeDifference = String.format("%02d:%02d:%02d",  hours, minutes, seconds);
        }else {
            timeDifference = String.format("%02d天 %02d:%02d:%02d", days, hours, minutes, seconds);
        }
        return timeDifference;
    }

    @ApiModelProperty(value = "0未删除，1已经删除")
    private Boolean delFlag;

    public void init(BusinessGoods goods, BusinessUser user, BusinessPlaceOrderParam dto, BigDecimal feesAmount){
        String code=goods.getCode()+user.getCode()+System.currentTimeMillis();
        this.setCode(code);

        this.setGoodsId(goods.getId());
        this.setGoodsCode(goods.getCode());
        this.setGoodsName(goods.getTitle());
        this.setGoodsAmount(goods.getAmount());
        this.setGoodsOperators(goods.getOperators());

        this.setBusinessUserId(user.getId());
        this.setBusinessUserCode(user.getCode());
        this.setBusinessUserName(user.getUserName());
        this.setBusinessUserNike(user.getNickName());

        this.setRechargeAccount(dto.getMobile());
        this.setFeesAmount(feesAmount);
        this.setState(OrderStatusEnum.init.getValue());
        this.setPayState(OrderPayStatusEnum.payment.getValue());
        this.setCreateTime(LocalDateTime.now());
        this.setBackUrl(dto.getBack_url());
        this.setUserOrderCode(dto.getSporderid());

    };

    public void init(BusinessGoods goods, BusinessUser user){

        this.setGoodsId(goods.getId());
        this.setGoodsCode(goods.getCode());
        this.setGoodsName(goods.getTitle());
        this.setGoodsAmount(goods.getAmount());
        this.setGoodsOperators(goods.getOperators());
        String code=goods.getCode()+user.getCode()+System.currentTimeMillis();
        this.setCode(code);

        this.setBusinessUserId(user.getId());
        this.setBusinessUserCode(user.getCode());
        this.setBusinessUserName(user.getUserName());
        this.setBusinessUserNike(user.getNickName());

        this.setFeesAmount(BigDecimal.ZERO);
        this.setState(OrderStatusEnum.init.getValue());
        this.setPayState(OrderPayStatusEnum.payment.getValue());
        this.setRemake("手动用户下单成功");
        this.setCreateTime(LocalDateTime.now());
    };
}
